package task3.pimpmypipe;

import java.io.StreamCorruptedException;

public abstract class AbstractSource<T>
  implements Readable<T>, Runnable
{
  private Writeable<T> m_Output = null;
  public static Object ENDING_SIGNAL = null;
  
  public AbstractSource() {}
  
  public AbstractSource(Writeable<T> output)
  {
    this.m_Output = output;
  }
  
  public void run() {}
  
  protected void writeOutput(T value)
    throws StreamCorruptedException
  {
    if (this.m_Output != null)
    {
      if (value == ENDING_SIGNAL) {
        beforeSendingEndingSignal();
      }
      this.m_Output.write(value);
    }
    else
    {
      throw new StreamCorruptedException("output is null");
    }
  }
  
  protected void sendEndSignal()
    throws StreamCorruptedException
  {
    writeOutput(null);
  }
  
  protected void beforeSendingEndingSignal()
    throws StreamCorruptedException
  {}
  
  public void setOutput(Writeable<T> output)
  {
    this.m_Output = output;
  }
}
